package net.lovesrc.support.service.mobileverifycode;

import com.zhouzhou.fastjava.BizException;
import com.zhouzhou.fastjava.http.Requests;
import com.zhouzhou.fastjava.util.StringKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HuoYunMobileVerifyCodeService extends AbstractMobileVerifyCodeService implements MobileVerifyCodeService {

    private static final Logger logger = LoggerFactory.getLogger(HuoYunMobileVerifyCodeService.class);

    public static final String DEFAULT_HOST = "http://embshop.net";

    private String username;

    private String password;

    private String gatewayUrl;

    public HuoYunMobileVerifyCodeService(String host, String username, String password) {
        this.username = username;
        this.password = password;
        this.gatewayUrl = host + "/api/do.php";
    }

    @Override
    protected String doGetVerifyCodeFromPhone(String itemId, String phone) {
        String body = Requests.post(gatewayUrl)
                .param("action", "getMessage")
                .param("token", getToken())
                .param("phone", phone)
                .param("sid", itemId)
                .execute()
                .asText();
        if (body.startsWith("1|")) {
            return getSuccessDataBody(body);
        }
        return null;
    }

    @Override
    public void addBlack(String itemId, String phone) {
        logger.info("Add Black Phone: {}", phone);
        Requests.post(gatewayUrl)
                .param("action", "addBlacklist")
                .param("token", getToken())
                .param("phone", phone)
                .param("sid", itemId)
                .execute()
                .asText();
    }

    @Override
    public void releasePhone(String itemId, String phone) {
        Requests.post(gatewayUrl)
                .param("action", "cancelRecv")
                .param("sid", itemId)
                .param("token", getToken())
                .param("phone", phone)
                .execute()
                .asText();
    }

    @Override
    public String getPhone(String itemId, String phone) {
        return getPhone(itemId,phone,null);
    }

    @Override
    public String getPhone(String itemId, String phone, String card) {
        String body = Requests.post(gatewayUrl)
                .param("action", "getPhone")
                .param("sid", itemId)
                .param("token", getToken())
                .execute()
                .asText();
        if (body.startsWith("1|")) {
            return StringKit.extract(getSuccessDataBody(body), "(\\d{11})");
        } else {
            throw BizException.messageOf("获取手机号失败");
        }
    }

    private String getSuccessDataBody(String body) {

        return body.substring("1|".length());
    }

    @Override
    protected String login() {
        String body = Requests.post(gatewayUrl)
                .param("action", "loginIn")
                .param("name", username)
                .param("password", password)
                .execute()
                .asText();
        if (body.startsWith("1|")) {
            return getSuccessDataBody(body);
        } else {
            throw BizException.messageOf("登录失败");
        }
    }

}
